#ifndef __LOG_SERVER_H
#define __LOG_SERVER_H

#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <sys/select.h>

#include <vector>

#include "SysLog.h"
#include "sqlite3.h"



class CLogServer
{
#define MAX_TCP_CLIENTS 20

public:
    CLogServer();
    ~CLogServer();

    int WhileMain();
    bool ProcessCmdFunc(const LOG_COMM_STU &recvStu, std::string &strRspData);

    static bool GetCurrentDateTime(std::string &strCurrentDate, std::string &strCurrentTime);

    int AddLogInLogSrv(LOG_LEVEL nLogLevel, const std::string &strSource, const char* logInfo, ...);

private:
    bool InitSocket();
    bool InitSqlLog();

    // sqlite3 Execute Function
    bool AddLogExecuteFunc(const LOG_COMM_STU &recvStu, std::string &strRspData);
    bool DelLogExecuteFunc(const LOG_COMM_STU &recvStu, std::string &strRspData);
    bool ClearLogExecuteFunc(const LOG_COMM_STU &recvStu, std::string &strRspData);
    bool QueryLogExecuteFunc(const LOG_COMM_STU &recvStu, std::string &strRspData);


    // Parse Message from the Server
    bool AddLogPraseMessage(const LOG_COMM_STU &commStu, LOG_LEVEL &logType, std::string &strObj, std::string &strDetailInfo);
    bool DelLogPraseMessage(const LOG_COMM_STU &commStu, LOG_LEVEL &logType, int &nCount);
    bool ClearLogPraseMessage(const LOG_COMM_STU &commStu, LOG_LEVEL &logType);
    bool QueryLogPraseMessage(const LOG_COMM_STU &commStu, LOG_LEVEL &logType, int &nCount, std::string &strKeyWord, std::string &strStartDateTime, std::string &strEndDateTime);


    // callback for the query result.
    static int QueryCountCallback(void* data, int argc, char** argv, char** azColName);
    static int QueryLogCallback(void* data, int argc, char** argv, char** azColName);

    sqlite3 *m_sqlFd;
    // TCP Client List
    pthread_mutex_t m_TcpCliArrayMutex;
    int m_clientSocket[MAX_TCP_CLIENTS];
    int m_sockFd;
    fd_set m_readfds;

    static int m_nCallBackIndex;
    static std::string m_strQueryRspData;

    pthread_mutex_t m_AddLogMutex;
    std::vector<LOG_COMM_STU> m_AddLogVt;

public:
    // Thread
    static void* AddLogThreadHandler(void * arg);
    static void* AcceptThreadHandler(void * arg);
    bool GetAllFromAddLogVt(std::vector<LOG_COMM_STU> &addLogVt);
    
    

};

#endif